﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace DusonFramework.Core.Web
{
    public class MessageBox
    {
        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(System.Web.UI.Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
            //page.RegisterStartupScript("message", "<script language='javascript' Ucren.alert('" + msg.ToString() + "','提示','function(){}');</script>");            
        }

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string msg)
        {
            //Control.Attributes.Add("onClick","if (!window.confirm('"+msg+"')){return false;}");
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("self.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", Builder.ToString());

        }
        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        public static void ResponseScript(System.Web.UI.Page page, string script)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "message", "<script language='javascript' defer>" + script + "</script>");
        }

        #region CloseWindows

        /// <summary>
        /// 关闭窗体,没有任何提示的关闭窗体
        /// </summary>

        public static void Close()
        {
            HttpContext.Current.Response.Write("<script>window.opener=null;window.close();</script>");
            HttpContext.Current.Response.End();
        }

        /// <summary>
        ///  有提示信息的关闭窗体
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="strMsg">弹出的确认信息</param>  

        public static void ConFirm_Close(System.Web.UI.Page page, string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("<script>if(confirm('{0}')==true){window.close();}</script>", strMsg));

            page.ClientScript.RegisterStartupScript(page.GetType(),
             System.Guid.NewGuid().ToString(), sb.ToString());
        }

        /// <summary>
        /// 有等待时间的关闭窗体
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="WaitTime">等待时间，以毫秒为记量单位</param>
        public static void Close_Wait(System.Web.UI.Page page, int WaitTime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">");
            //加入此行功能后没有提提示功能
            sb.Append("window.opener=null;");
            sb.Append("setTimeout");
            sb.Append("(");
            sb.Append("'");
            sb.Append("window.close()");
            sb.Append("'");

            sb.Append(",");
            sb.Append(WaitTime.ToString());
            sb.Append(")");
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(),
                        System.Guid.NewGuid().ToString(), sb.ToString());

        }

        #endregion

        #region  ShowStatusBar

        /// <summary>
        /// 将消息显示在状态栏
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="strMsg">显示在状态栏的消息</param>
        public static void ShowStatus(System.Web.UI.Page page, string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">");
            sb.Append("window.status=");
            sb.Append("\"");
            sb.Append(strMsg);
            sb.Append("\"");
            sb.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(),
                System.Guid.NewGuid().ToString(), sb.ToString());
        }

        #endregion

        /// <summary>
        /// 重新加载页面（通常用于弹出的opener刷新父页面）
        /// </summary>
        public static void ReLoad()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">");
            sb.Append("window.location.reload(true);");
            sb.Append("</script>");
            HttpContext.Current.Response.Write(sb.ToString());
            HttpContext.Current.Response.End();

        }

        /// <summary>
        /// 执行一个JS语句
        /// </summary>
        /// <param name="page">页面指针,一般为This</param>
        /// <param name="jsFunc">要执行的语句</param>
        public static void ExecuteJs(System.Web.UI.Page page, string jsFunc)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "ExecuteJs", "<script>" + jsFunc + "</script>");

        }

        /// <summary>
        /// 关闭模态对话框，给父页面返回一个returnvalue=true的值，用于决定父页面是否执行后台js
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        public static void SaveData(System.Web.UI.Page page)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "alert", "<script>window.returnValue=true;window.close();</script>");

        }

        #region 不提示信息只返回原页面

        /// <summary>
        /// 回退到上一个页面
        /// </summary>
        public static void GoBack()
        {

            HttpContext.Current.Response.Write("<script language='javascript'>window.history.go(-2);</script>");
            HttpContext.Current.Response.End();
        }
        #endregion

    }
}
